package com.hw.ui.config;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.hw.HWApp;
import com.hw.R;
import com.hw.base.activity.BaseActivity;
import com.hw.base.util.Util;
import com.hw.daobean.DataBaseControl;
import com.hw.tool.ToastUtils;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConfigActivity extends BaseActivity<ConfigModel, ConfigView, ConfigPresent> implements ConfigView {
    private String tag = "ConfigActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.ivu)
    ImageView ivu;
    @Bind(R.id.login_id)
    EditText loginId;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.tvr)
    TextView tvr;
    @Bind(R.id.loginbt)
    Button loginbt;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.baseinfbt)
    Button baseinfbt;
    @Bind(R.id.clearbt)
    Button clearbt;
    String cid = "";
    String add = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);
        ButterKnife.bind(this);
        tvTitle.setText("网络配置");
        loginId.setText(HWApp.sSharedPreferences.getString(Util.cidk, ""));
        loginP.setText(HWApp.sSharedPreferences.getString(Util.addk, "").replace("http://",""));
//        loginId.setText(HWApp.sSharedPreferences.getString(Util.cidk, "9999"));
//        loginP.setText(HWApp.sSharedPreferences.getString(Util.addk, "pid.zaosin.cn").replace("http://",""));
//        loginP.setText(HWApp.sSharedPreferences.getString(Util.addk, "bjp.nepbaby.com:8089").replace("http://",""));
    }

    @Override
    public ConfigModel createModel() {
        return new ConfigModelImpl();
    }

    @Override
    public ConfigView createView() {
        return this;
    }

    @Override
    public ConfigPresent createPresenter() {
        return new ConfigPresent();
    }

    @Override
    public void config(String str) {
        tvr.setText(str);
    }

    @Override
    public void download(String str) {
        tvr.setText(str);
    }

    @OnClick({R.id.iv_title_left, R.id.loginbt, R.id.baseinfbt, R.id.clearbt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.loginbt:
                cid = loginId.getText().toString();
                if (cid.length() == 0) {
                    showMsg("请填写企业编号");
                    return;
                }
                add = loginP.getText().toString();
                if (add.length() == 0) {
                    showMsg("请填写服务器地址");
                    return;
                }
                presenter.config("http://"+add, cid);
                break;
            case R.id.baseinfbt:
                cid = HWApp.sSharedPreferences.getString(Util.cidk, "");
                add = HWApp.sSharedPreferences.getString(Util.addk, "");
                if(cid.length() == 0 || add.length() == 0)
                {
                    ToastUtils.showSingleToast(this, "下载前请先完成网络配置");
                    return;
                }
                presenter.download();
                break;
            case R.id.clearbt:
                DataBaseControl.deleteRelationAlls();
                DataBaseControl.deleteUpRelationAlls();
                showMsg("清空条码完成");
                break;
        }
    }
}
